<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <script src="./layui/layui.js"></script>
    <script src="./common.js"></script>
    <style>
        form {
            background-color: wheat;
            width: 200px;
            height: 400px;
            margin: 0 auto;
            padding-left: 10px;
        }

        input {
            outline: none;
        }
    </style>
</head>

<body>
    <form class="layui-form" action="">
        <h2>登录页面</h2>
        <div class="layui-form-item">
            <label class="layui-form-label">手机号</label>
            <div class="layui-input-block">
                <input type="text" name="title" required lay-verify="required" placeholder="请输入手机号" autocomplete="off"
                    class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码框</label>
            <div class="layui-input-inline">
                <input type="password" name="password" required lay-verify="required" placeholder="请输入密码"
                    autocomplete="off" class="layui-input">
            </div>
        </div>
        <br>
        <div class="layui-form-item">
            <label class="layui-form-label">七天免登录</label>
            <div class="layui-input-block">
                <input type="checkbox" name="switch" lay-skin="switch" lay-text="switch">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="button" class="layui-btn login" lay-submit="" lay-filter="demo1">登录</button>
                <button type="reset" class="layui-btn layui-btn-primary">重新输入</button>
            </div>
        </div>
    </form>

    <script>
        // 点击事件
        document.querySelector("button").onclick = function () {
            // 获取元素
            let mobile = document.querySelector("input[name='title']").value;
            let password = document.querySelector("input[name='password']").value;
            //   一、创建请求
            let xhr = new XMLHttpRequest;
            // 设置请求方式，建立连接
            xhr.open('POST', 'http://phpclub.org.cn/edu/server/login.php');
            // 创建请求头
            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            // send发送连接,传送属性值
            xhr.send(`mobile=${mobile}&password=${password}`);
            xhr.onload = function () {
                let response = JSON.parse(xhr.responseText);
                // 后端规定code为100表示数据为空，
                if (response.code != 100){
                    // layer.msg属于layui框架内置所封装的功能
                    layer.msg(response.msg, {
                        icon: 5
                    });
                }
                // 输入参数的情况 
                else {
                    //response.data 返回的全部数据
                    // '名称=值;path=地址;expires=时间'
                    setCookie("userInfo", JSON.stringify(response.data))//将js中内置的数据类型(数组、对象)转换为JSON格式的字符串
                    // 在首页通过// JSON.parse(getCookie('userInfo')将JSON格式的字符串转换为内置的数据类型
                       console.log(response.data);
                    //    console.log(userInfo);
                    // let callback=getUrlParam('callback')?decodeURIComponent(getUrlParam('callback')):'content.html'
                    // location.href = callback;
                    location.href="index_pro.html"
                }
            }
        }
    </script>
</body>
</html>